package com.example.matchandroiddevices.scanlable;

import android.app.Activity;
import android.content.Intent;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.example.matchandroiddevices.R;

import java.util.ArrayList;
import java.util.HashMap;
public class Connect232 extends Activity {

	private static final String TAG = "COONECTRS232";
	private static final boolean DEBUG = true;
	private TextView mConectButton;

	private EditText   ipAddr,ipPort;
    private String strIP;
    private String strPort;
    
    Spinner spType;
	Spinner spCom;
	Spinner spBaud;

	String[]devname = new String[40];
	String[]BaudRate = new String[4];
	String devicePath="/dev/bus/usb";
	int speed =19200;
	LinearLayout layout_rs232;
	LinearLayout layout_tcp;
	int mType=0;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_connect232);
		getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
		ipAddr =(EditText)findViewById(R.id.editIp);
		ipPort =(EditText)findViewById(R.id.editPort);
		layout_rs232 =(LinearLayout)findViewById(R.id.layout_rs232);
		layout_tcp = (LinearLayout)findViewById(R.id.layout_tcp);
		layout_tcp.setVisibility(View.GONE);
		
		spType = (Spinner)findViewById(R.id.comtype_spinner);
		ArrayAdapter<CharSequence> adapter =  ArrayAdapter.createFromResource(this, R.array.comtype_spinner, android.R.layout.simple_spinner_item);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spType.setAdapter(adapter); 
		spType.setSelection(0, true);
		spType.setOnItemSelectedListener(new ComOnItemSelectedListener());
		initSound();
		devname[0]="/dev/bus/usb/001/002";
		devname[1]="/dev/ttyUSB0";
		devname[2]="/dev/ttymxc0";
		devname[3]="/dev/ttymxc1";
		devname[4]="/dev/ttymxc2";
		devname[5]="/dev/ttymxc3";
		devname[6]="/dev/ttymxc4";
		devname[7]="/dev/ttyS0";
		devname[8]="/dev/ttyS1";
		devname[9]="/dev/ttyS2";
		devname[10]="/dev/ttyS3";
		devname[11]="/dev/ttyS4";
		devname[12]="/dev/ttyS6";
		devname[13]="/dev/ttyMT0";
		devname[14]="/dev/ttyMT1";
		devname[15]="/dev/ttyMT2";
		devname[16]="/dev/ttyMT3";
		devname[17]="/dev/ttyHSL0";
		devname[18]="/dev/ttyHSL1";
		devname[19]="/dev/ttyHSL2";
		devname[20]="/dev/ttyHSL3";
		devname[21]="/dev/ttyUSB2";
		devname[22]="/dev/ttyUSB3";
		devname[23]="/dev/ttyUSB4";
		devname[24]="/dev/ttyUSB5";
		devname[25]="/dev/ttyUSB6";
		devname[26]="/dev/ttyUSB7";
		devname[27]="/dev/ttyUSB8";
		devname[28]="/dev/ttyUSB9";
		devname[29]="/dev/ttyUSB10";
		devname[30]="/dev/ttyUSB11";
		devname[31]="/dev/ttyUSB12";
		devname[32]="/dev/ttyUSB13";
		devname[33]="/dev/ttyUSB14";
		devname[34]="/dev/ttyUSB15";
		devname[35]="/dev/ttyUSB16";
		devname[36]="/dev/ttyUSB17";
		devname[37]="/dev/ttyUSB18";
		devname[38]="/dev/ttyUSB19";
		devname[39]="/dev/ttyUSB20";
		spCom = (Spinner)findViewById(R.id.rs232_spinner);
		ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,devname);
		adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spCom.setAdapter(adapter1); 
		spCom.setSelection(0, true);
		spCom.setOnItemSelectedListener(new ComOnItemSelectedListener());
		
		BaudRate[0]="19200";
		BaudRate[1]="38400";
		BaudRate[2]="57600";
		BaudRate[3]="115200";
		spBaud = (Spinner)findViewById(R.id.baud_spinner);
		ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,BaudRate);
		adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spBaud.setAdapter(adapter2); 
		spBaud.setSelection(0, true);
		spBaud.setOnItemSelectedListener(new ComOnItemSelectedListener());
		
		mConectButton = (TextView) findViewById(R.id.textview_connect);
		mConectButton.setOnClickListener(v -> {
            try {
                int result=0x30;
                if(mType==0)
                {
                    result = HfData.reader.OpenReader(speed, devicePath, mType, 1, null);
                }
                else
                {
                    try{
                        if(ipAddr.getText().equals(null))strIP="192.168.1.192";
                        else strIP=ipAddr.getText().toString();

                        if(ipPort.getText().equals(null))strPort="6000";
                        else strPort=ipPort.getText().toString();

                    }catch(Exception ex)
                    {strIP="192.168.1.192";strPort="6000";}
                    int nPort=Integer.valueOf(strPort);
                    result = HfData.reader.OpenReader(nPort, strIP, mType, 1, null);
                }
                if(result==0){
                    InventoryTagMap map = new InventoryTagMap();//榛樿鍚敤绗竴涓ぉ绾跨洏鐐�
                    map.Antenna = 1;
                    map.isCheck =true;
                    map.newlist = new ArrayList<HashMap<String, String>>();
                    map.oldlist = new ArrayList<HashMap<String, String>>();
                    map.autolist = new ArrayList<HashMap<String, String>>();
                    HfData.mlist.add(map);
                    Intent intent;
                    intent = new Intent().setClass(Connect232.this, MainHFActivity.class);
                    startActivity(intent);
                }
                else
                {
                    Toast.makeText(
                            getApplicationContext(),
                            "连接失败",
                            Toast.LENGTH_SHORT).show();
                }
            }catch (Exception e)
            {
                Toast.makeText(
                        getApplicationContext(),
                        "连接失败",
                        Toast.LENGTH_SHORT).show();
            }
        });
		}
		

		public class ComOnItemSelectedListener implements OnItemSelectedListener{

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				// TODO Auto-generated method stub
                int id = arg0.getId();
                if (id == R.id.comtype_spinner) {
                    mType = arg2;
                    if (arg2 == 0) {
                        layout_rs232.setVisibility(View.VISIBLE);
                        layout_tcp.setVisibility(View.GONE);
                    } else {
                        layout_rs232.setVisibility(View.GONE);
                        layout_tcp.setVisibility(View.VISIBLE);
                    }
                } else if (id == R.id.rs232_spinner) {
                    devicePath = devname[arg2];
                } else if (id == R.id.baud_spinner) {
                    if (arg2 == 0)
                        speed = 19200;
                    else if (arg2 == 1)
                        speed = 38400;
                    else if (arg2 == 2)
                        speed = 57600;
                    else if (arg2 == 3)
                        speed = 115200;
                }
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
				
			}
		}
		
		
		
		public boolean onKeyDown(int keyCode, KeyEvent event) {
			if (keyCode == KeyEvent.KEYCODE_BACK) {
				
				finish();

				return true;
			}
			return super.onKeyDown(keyCode, event);
		}
		
		@Override
		protected void onDestroy() {
			// TODO Auto-generated method stub
			super.onDestroy();
		}
		
		static HashMap<Integer, Integer> soundMap = new HashMap<Integer, Integer>();
		private static SoundPool soundPool;
		private static float volumnRatio;
		private static AudioManager am;
		private  void initSound(){
			soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 5);
			soundMap.put(1, soundPool.load(this, R.raw.msg, 1));
			am = (AudioManager) this.getSystemService(AUDIO_SERVICE);// 瀹炰緥鍖朅udioManager瀵硅薄
			ScanMode.soundid = soundMap.get(1);
			ScanMode.soundPool = soundPool;
		}

}
